package com.moli.iov.admin.websocket.service.query.impl;


import com.moli.iov.admin.model.vo.res.VehicleAuthVo;
import com.moli.iov.admin.utils.ParamUtil;
import com.moli.iov.admin.websocket.model.MonitorDetailResVo;
import com.moli.iov.admin.websocket.service.AbstractWebSocketQueryService;
import com.moli.iov.admin.websocket.service.WebSocketQueryService;
import com.moli.iov.base.model.dto.req.VehicleBaseAuthReqDto;
import com.moli.iov.base.model.dto.res.VehicleBaseAuthResDto;
import com.moli.iov.base.model.dto.res.VehicleBaseTboxResDto;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.remote.IVehicleBaseAuthRemoteService;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.base.remote.IVehicleTboxRemoteService;
import com.moli.iov.protocol.model.dto.realtime.RealTimeInfoDto;
import com.moli.iov.protocol.model.realtime.RealTimeInfo;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import com.moli.iov.protocol.util.RealTimeInfoUtil;
import com.moli.iov.real.enums.OnlineTag;
import com.moli.iov.real.model.res.VehicleStatusDto;
import com.moli.iov.real.service.IRealService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MonitorDetailWServiceImpl
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-03-06 上午 9:53
 * @UpdateDate 2019-03-06 上午 9:53
 * @Version 1.0
 */
@Slf4j
@Service("monitorDetailWService")
public class MonitorDetailWServiceImpl extends AbstractWebSocketQueryService<String> implements WebSocketQueryService {

    @Reference(group = "${service.group}", check = false)
    IRealService realService;

    @Reference(group = "${service.group}", check = false)
    IVehicleBaseRemoteService vehicleBaseRemoteService;

    @Reference(group = "${service.group}", check = false)
    IVehicleTboxRemoteService vehicleTboxRemoteService;

    @Reference(group = "${service.group}", check = false)
    IVehicleBaseAuthRemoteService vehicleBaseAuthRemoteService;

    @Override
    public String buildParam(String string) throws Exception {
        return JsonUtils.parse(string,String.class);
    }

    @Override
    public RestResponse get(String vin,String token) {

        log.info("[车辆监控-查询车辆实时信息][vin:{}]", vin);

        if (StringUtils.isEmpty(vin)) {
            return RestResponse.failure("vin不能为null ！！！");
        }

        //返回Data
        MonitorDetailResVo resVo = new MonitorDetailResVo();

        VehicleBase vehicleDto = vehicleBaseRemoteService.getVehicleBaseByVin(vin);
        VehicleBaseTboxResDto vehicleTbox = vehicleTboxRemoteService.getVehicleTboxByVin(vin);

        RestResponse<RealTimeInfo> realTimeInfo = null;
        try {
            realTimeInfo = realService.getRealTime(vin);
        } catch (Exception e) {
            log.error("查询车辆实时报文异常,车架号:{}", vin);
        }

        RestResponse<Boolean> onlineRes = null;
        try {
            onlineRes = realService.isOnline(vin);
        } catch (Exception e) {
            log.error("查询车辆在线状态异常,车架号:{}", vin);
        }

        if (vehicleDto != null) {
            resVo.setLicense(ParamUtil.getParam(vehicleDto.getLicense()));  //车牌号
            resVo.setVin(vehicleDto.getVin()); //车架号
            resVo.setSimNo(ParamUtil.getParam(vehicleTbox.getSimNo())); //sim卡号
            resVo.setDeviceId(ParamUtil.getParam(vehicleTbox.getDeviceId()));  //设备id
            resVo.setOrganizationInfo(ParamUtil.getParam(vehicleDto.getOrganizationInfo())); //门店名称

            //品牌
            resVo.setBrandName(vehicleDto.getBrandName());
            //车系
            resVo.setVehicleSeries(vehicleDto.getVehicleSeriesName());
            //车型
            resVo.setVehicleType(vehicleDto.getVehicleTypeName());
            //城市
//            resVo.setRegionName(dictDataFacadeService.getRegionName(vehicleBaseTboxResDto.getCityId()));

        }

        //车辆在线状态
        if (onlineRes != null && onlineRes.isFlag() && onlineRes.getData() != null) {
            resVo.setOnlineStatus(ParamUtil.getParam(OnlineTag.getKey(onlineRes.getData())));
        }

        if (realTimeInfo != null && realTimeInfo.isFlag() && realTimeInfo.getData() != null && realTimeInfo.getData().getRealTimeInfoReq()!=null) {
            RealTimeInfoReq realDto = realTimeInfo.getData().getRealTimeInfoReq();
            //上报时间
            resVo.setReportDate(realDto.getCollectDate().getDate());
            //动力状态
            resVo.setDynamicStatus(RealTimeInfoUtil.getDynamicStatus(realDto.getExtendReq().getextendVehicleState()));
            //钥匙状态
            resVo.setKeyStatus(RealTimeInfoUtil.getKeyStatus(realDto.getExtendReq().getextendVehicleState()));
            //租赁状态
            resVo.setLeasingStatus(RealTimeInfoUtil.getLeasingStatus(realDto.getExtendReq().getextendVehicleState()));
            //是否定位
            resVo.setLocationStatus(RealTimeInfoUtil.getLocationStatusK(realDto.getVehicleGpsReq()));

            resVo.setLongitude(RealTimeInfoUtil.getLongitude(realDto.getVehicleGpsReq()));
            resVo.setLatitude(RealTimeInfoUtil.getLatitude(realDto.getVehicleGpsReq()));
            resVo.setGpsOde(RealTimeInfoUtil.getGpsOde(realTimeInfo.getData()));

            resVo.setOdometer(RealTimeInfoUtil.getOdometer(realDto.getVehicleAllReq()));

            if(realDto.getExtendReq()!=null){
                resVo.setPowerGear(RealTimeInfoUtil.getPowerGear(realDto.getExtendReq().getextendVehicleState()));
            }
        }

        Double realOdo = getRealOdo(vin);
        resVo.setRealOdo(realOdo);
        VehicleAuthVo authVo = getAuthStatus(vin,vehicleTbox);
        resVo.setVehicleAuthVo(authVo);
        return RestResponse.success(resVo);
    }

    private Double getRealOdo(String vin ){
        List<String> list = new ArrayList<>();
        list.add(vin);
        RestResponse<Map<String, VehicleStatusDto>> res = realService.getVehicleStatusByVin(list);
        if(res!=null && res.getData()!=null && res.getData().get(vin)!=null){
            return res.getData().get(vin).getOdo();
        }
        return 0D;
    }

    public VehicleAuthVo getAuthStatus(String vin, VehicleBaseTboxResDto vehicleTbox){
        VehicleBaseAuthReqDto dto = new VehicleBaseAuthReqDto();
        dto.setVin(vin);
        RestResponse<VehicleBaseAuthResDto> auth = vehicleBaseAuthRemoteService.getVehicleBaseAuth(dto);
        VehicleAuthVo authVo = new VehicleAuthVo();
        if(auth!=null && auth.flag){
            CommonUtils.copyProperties(auth.getData(),authVo);
        }
        if(vehicleTbox!=null){
            CommonUtils.copyProperties(vehicleTbox,authVo);
        }
        return authVo;
    }


}
